package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zsk
 * @create 2022-03-04-16:52
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //查询现有菜品(分页查询)
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //设置查询条件(可能根据姓名模糊查询)
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //where name like = "%#{name}%"
        wrapper.like(!StringUtils.isEmpty(name),Dish::getName,name);

        //设置分页条件
        Page<Dish> page = new Page<>(pageNum,pageSize);

        //分页查询(调用mapper层查询)
        page = dishMapper.selectPage(page, wrapper);
        System.out.println(page);

        //获取分页查询的结果
        List<Dish> dishList = page.getRecords();
        //如果不为空
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //根据id获取当前菜品的分类信息
                Category category = categoryMapper.selectById(dish.getCategoryId());
                //存储到dish表中的categoryName字段中
                dish.setCategoryName(category.getName());

                //获取当前菜品的分类信息
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());

                //调用dishFlavorMapper查询
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);

                //存储到dish里的flavors字段中
                dish.setFlavors(dishFlavorList);
            }
        }
        return page;
    }

    //新增菜品数据
    @Override
    public void save(Dish dish) {
        log.info("保存之前的dish对象：" +dish);
        dishMapper.insert(dish);
        log.info("保存之后的dish对象：" +dish);

        //保存菜品的口味信息
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)){
            for (DishFlavor dishFlavor : dishFlavorList) {

                //设置保存菜品的id
                dishFlavor.setDishId(dish.getId());
                //保存口味信息
                dishFlavorMapper.insert(dishFlavor);

            }
        }
    }

    //根据id查询菜品信息
    @Override
    public Dish findById(Long id) {

        //调用mapper层查询
        Dish dish = dishMapper.selectById(id);

        //根据id查询菜品口味信息
        //构建查询条件
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        //调用dishFlavorMapper查询
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        //存储到dish里的Flavors字段中
        dish.setFlavors(dishFlavorList);

        return dish;
    }

    //修改菜品信息
    @Override
    public void update(Dish dish) {
        //调用mapper层修改
        dishMapper.updateById(dish);

        //先删除原有菜品的口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //再保存新的口味信息
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)){
            for (DishFlavor dishFlavor : dishFlavorList) {
                //设置菜品口味id
                dishFlavor.setDishId(dish.getId());
                //保存口味信息
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    //修改菜品状态
    @Transactional
    @Override
    public void updateStatus(Integer status, Long[] ids) {

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);

            //调用mapper层保存
            dishMapper.updateById(dish);
        }

    }

    //删除菜品信息
    @Transactional//事物控制
    @Override
    public void deleteById(Long[] ids) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);//where id = #{id}
        wrapper.eq(Dish::getStatus,1);//where status = 1
        //调用mapper层查询
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0){
            throw new CustomException("当前菜品处于在售状态，不允许删除");
        }
        //遍历返回的ids
        for (Long id : ids) {
            //构建删除条件
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,id);
            //执行删除口味信息
            dishFlavorMapper.delete(wrapper1);//这是一个条件
            //删除菜品信息
            dishMapper.deleteById(id);
        }
    }
}
